package com.htx.oldbookpavilion.service.homeservice.impl;

import com.baidu.fsg.uid.UidGenerator;
import com.htx.oldbookpavilion.entity.ShoppingList;
import com.htx.oldbookpavilion.entity.ShoppingListExample;
import com.htx.oldbookpavilion.mapper.ShoppingListMapper;
import com.htx.oldbookpavilion.service.homeservice.BookDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BookDetailServiceImpl implements BookDetailService {

    @Autowired
    private UidGenerator uidGenerator;

    @Autowired
    private ShoppingListMapper shoppingListMapper;

    /**
     * 将指定书籍添加到购书清单
     * @param shoppingList
     * @return
     */
    @Override
    public boolean addToShoppingList(ShoppingList shoppingList) {
        long shoppingListId = uidGenerator.getUID();
        shoppingList.setShoppingListId(shoppingListId);
        shoppingList.setCreateTime(new Date());
        int insert = shoppingListMapper.insert(shoppingList);
        if(insert > 0){
            return true;
        }
        return false;
    }

    /**
     * 校验该书籍是否已经添加到购书清单
     * @param shoppingList
     * @return
     */
    @Override
    public boolean isAddToShoppingList(ShoppingList shoppingList) {
        ShoppingListExample shoppingListExample = new ShoppingListExample();
        shoppingListExample.or().andUserInfoIdEqualTo(shoppingList.getUserInfoId())
                .andBookIdEqualTo(shoppingList.getBookId());
        List<ShoppingList> shoppingLists = shoppingListMapper.selectByExample(shoppingListExample);
        if (shoppingLists == null || shoppingLists.size() == 0) {
            return true;
        }
        return false;
    }
}
